home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / Sample Code Update 01⁄96 / ChromaKeyMovie / Read Me Chromakey Movie next >
Encoding:
Text File  |  1995-11-20  |  15.1 KB  |  292 lines  |  [TEXT/ttxt]

  1. Chromakey Movie
  2.  
  3. Written by:    Jason Hodges-Harris,  Developer Technical Support
  4.     
  5. Copyright:    © 1995 by Apple Computer, Inc. All rights reserved.
  6.  
  7.  
  8. Version History
  9.  
  10. 1.0d1   -   09/--/95   JHH.
  11.    Initial release version.
  12.  
  13. What it does
  14.  
  15. Chromakey Movie is a simple application which is designed to show alternative approaches to removing a color from a QuickTime movie while it is playing and allowing a separate image to be displayed in the removed regions. It demonstrates several features available in QuickTime and Color QuickDraw:
  16.  
  17. Color Quickdraw
  18.  
  19. •   The use of GWorlds to store the current QuickTime movie frame, the background image and the composite (movie frame and background) image.
  20.  
  21. •   Various arithmetic transfer modes with CopyBits() to manipulate image data between offscreen graphics ports and a visible window.
  22.  
  23.  
  24. QuickTime
  25.  
  26. •   The playing of a movie into an offscreen port and transfer to a visible window.
  27.  
  28. •   The use of a modifier track to provide additional information as to the display characteristics of the video tracks in the movie.
  29.  
  30. •   Operation of the SetVideoMediaGraphicsMode() function to provide a Chromakeying effect. 
  31.  
  32. •   The use of the movie controller to manipulate the playback of the movie.
  33.  
  34.  
  35. Additional features demonstrated
  36.  
  37. •   Checking in a PowerPC native application if a system feature is available from a weak linked shared library as Gestalt() doesn't always provide the correct response.
  38.  
  39.  
  40. •   The implementation of the Dialog manager GetStdFilterProc() and SetDialogDefaultItem() functions to provide default alert and warning dialogs with highlighting of the default button. These functions are not documented within Inside Macintosh, but have been available since System 7.0 and are fully explained in the Technical Note "Toolbox - TB 37 - Pending Update Perils".
  41.  
  42.  
  43. How to use "Chromakey Movie"
  44.  
  45. After launching the application, the menubar contains four titles:  File, Edit, Keying Mode and Movie Options.
  46.  
  47.  
  48. File menu.
  49.  
  50. •   Open. This opens a QuickTime movie file, and displays it in a window together with a standard movie controller bar. As only one open window is permitted in the application, this menu item is disabled until the window is closed.
  51.  
  52. •   Close. This closes the movie window and therefore is only enabled when a window is visible.
  53.  
  54. •   Quit. When selected exits the application.
  55.  
  56.  
  57. Edit menu.
  58.  
  59. All of these menu items are disabled as the application does not currently support these operations.
  60.  
  61.  
  62. Keying Mode menu.
  63.  
  64. •   Transparent Color. This uses the standard color picker to select the color to remove either from the QuickTime movie file when in the foreground or the background image when the movie is in the background. This allows regions of the background to become visible when the movie is in the foreground and regions of the movie to become visible when the movie is in the background.
  65.  
  66. •   Movie in Background. Sends the Quicktime movie behind the background image.
  67.  
  68. •   Movie in Foreground. Places the QuickTime movie in the foreground.
  69.  
  70. •   Transparent Color Transfer. This selects a transfer method which performs the following steps:
  71.             (i)   Creates a GWorld and by calling SetMovieGWorld(), draws the QuickTime movie into the offscreen GWorld.
  72.             (ii)  Using CopyBits() set to the transparent transfer mode, merges the current movie frame and a background
  73.                    image together before transferring the result into a visible window.
  74.  
  75. •   Graphix Mode. This option calls SetVideoMediaGraphicsMode() to sets all video track media to the transparent transfer mode  with the key color selected in the Transparent Color menu option. See Inside Macintosh QuickTime page 2-287 for further information about this function.
  76.  
  77. •   Modifier Track. This function is specific to versions of QuickTime from 2.1 and this menu item is disabled  on Macintoshes running earlier versions. It provides similar functionality to the method implemented in the Graphix Mode menu item. See the reference documentation supplied with QuickTime 2.1 for further details on modifier tracks.
  78.  
  79.  
  80. Movie Options menu.
  81.  
  82. •   Play Every Frame. Selecting this option forces the movie controller to play each frame of the movie however slowly. As a 
  83.        side effect, this causes any audio track to be disabled.
  84.  
  85. •   Loop movie. Selecting this option will continually loop the playback of the movie.
  86.  
  87.  
  88.  
  89. Building the application
  90.  
  91. "Chromakey Movie" is provided with project files and make scripts for 68k and Power PC native versions in the following environments:
  92.  
  93.     Metrowerks CodeWarrior 7
  94.     Symantec C++ 8.0.1
  95.     Symantec 7.0.4
  96.     MPW E.T.O. #18- 'Latest MPW': MPW C, PPCC, Symantec C++ for MPW and MrC.
  97.  
  98. The Symantec environments are using a slightly older version of the Universal Interfaces than MPW and CodeWarrior. You will need to use a later version of the Universal Interfaces than is provided with the Symantec products, or make some changes to the source code. To change the Universal Interfaces, simply place brackets around the existing folder and place the folder containing the later version into the same folder as the existing ones. The brackets will prevent the development environment from using the files contained within the old folder.
  99.  
  100. Two MPW make files are included: to build a 68K and a 'fat' version using 'Latest MPW'. The 'Latest MPW' set up defaults to using the MPW C and PPCC compilers, but you can change the {C} and {PPCC} variables to SC and MrC if you have these compilers installed. You will need to alter the compiler options as appropriate and use the 'Latest MPW' interfaces and libraries rather than SCLibraries and SCIncludes. To build with 'PreRelease MPW' create a make file using 'Create Build Commands' and ensure that 'qd' is defined in all cases, since the PreRelease MPW Libraries do not define 'qd'. 
  101.  
  102. Please note, that this application requires the universal headers and shared library (for Power PC build) supplied with  QuickTime 2.1. These can be found within the Interfaces folder of the QuickTime 2.1 developer's kit.
  103.  
  104.  
  105. Application Files
  106.  
  107. doevent.cp
  108. •   Handles the processing of application events.
  109.  
  110. eventloop.cp
  111. •   Performs the program's wait next event loop.
  112.  
  113. menu.cp
  114. •   Handles the processing of the menu options. This includes initialisation, calling application functions to perform the selected options and enabling/disabling menu items.
  115.  
  116. start.cp
  117. •   Performs the initialisation of the application setting the default values for the global variables, initialising the toolbox managers, testing for the availability and version of QuickTime and calling the main application loop.
  118.  
  119. Test.cp
  120. •   This contains all of the files which handle the three alternative methods of producing the chromakeying effect on the movie.
  121.  
  122. windows.cp
  123. •   This performs the functions relating to opening, dragging, closing and updating the movie window and also the display of the generic alert dialogs.
  124.  
  125. ChromaKeyMovie.r
  126. •   Contains information to specific to MPW to enable the Rez tool to append the ChromaKeyMovie.rsrc file.
  127.  
  128. ChromaKeyMovie.rsrc
  129. •   This file contains the application resources.
  130.  
  131. ChromaKeyMovie.app.h
  132. •   The application's constants and window document structure are defined within this file.
  133.  
  134. ChromaKeyMovie.protos.h
  135. •   The application's function prototypes for all of the source code .cp files are defined within this file.
  136.  
  137.  
  138.  
  139. How the interesting functions operate and window data storage format
  140.  
  141.  Transparent Color Transfer mode.
  142.  
  143. The main work for this function is supplied by the TransparentKeyMode() function, which is located in the moviecontrol.c file. When this transfer mode is selected, the QuickTime movie is redirected into an offscreen GWorld, after which the background image and the movie are composited together in a separate GWorld before the final frame is transferred to the screen. 
  144.  
  145. The following code is taken from the TransparentKeyMode() function:
  146.  
  147. •    First the background image is transferred into a separate GWorld for compositing. This image is held in a separate GWorld and by keeping it in memory, reduces the overhead of constantly locating and reloading the image for each frame. 
  148.  
  149. In this application the background image is automatically resized to fit the movie GWorld and therefore distorts the background image. 
  150.  
  151.  SetGWorld(gBackGroundPort,nil);
  152.     RGBForeColor(&kRGBBlack);
  153.     RGBBackColor(&kRGBWhite);
  154.     CopyBits((BitMap*)(*gBackGndPictPM),
  155.         (BitMap*)(*gBackGndPixmap),
  156.         &(*gBackGroundPicture).portRect,&theRect,srcCopy,nil);
  157.  
  158.  
  159. •    A test is performed on whether the movie frame is displayed in front or behind the background image. If true the next section sets the GWorld containing the movie frame background color to that required as the keying color and then using CopyBits(), transfers the background image into that GWorld. The composite image is then transferred to the visible window using CopyBits() set to the srcCopy mode.
  160.  
  161. In this application if the key color is set to white, will produce a black check pattern with the movie displayed in the portions of the background image which are white.  
  162.  
  163.         SetGWorld(gOffscreenPort,nil);
  164.         RGBForeColor(&kRGBBlack);
  165.         RGBBackColor(&gKeyColor);
  166.         CopyBits((BitMap*)(*gBackGndPixmap),
  167.             (BitMap*)(*gMoviePixmap),
  168.             &theRect,&theRect,transparent,nil);
  169.         SetGWorld((CWindowPtr)theWindow,oldDevice);
  170.         RGBForeColor(&kRGBBlack);
  171.         RGBBackColor(&kRGBWhite);
  172.         CopyBits((BitMap*)(*gMoviePixmap),
  173.             (BitMap*)&((*theWindow).portBits),
  174.             &theRect,&theRect,srcCopy,nil);
  175.  
  176.  
  177.     • If the movie ius set to display in front of the background image then the effect produced is dependent on the movie being played. This transfers the GWorld containing the movie frame onto the background image and transfers the composite image to the screen. If the movie frame contains the same color at a given point as that of the key color, then this is replaced by the background image at the corresponding location.
  178.  
  179.   SetGWorld(gBackGroundPort,nil);
  180.         RGBForeColor(&kRGBBlack);
  181.         RGBBackColor(&gKeyColor);
  182.         CopyBits((BitMap*)(*gMoviePixmap),
  183.             (BitMap*)(*gBackGndPixmap),
  184.             &theRect,&theRect,transparent,nil);
  185.         SetGWorld((CWindowPtr)theWindow,oldDevice);
  186.         RGBForeColor(&kRGBBlack);
  187.         RGBBackColor(&kRGBWhite);
  188.         CopyBits((BitMap*)(*gBackGndPixmap),
  189.             (BitMap*)&((*theWindow).portBits),
  190.             &theRect,&theRect,srcCopy,nil);
  191.  
  192.  
  193. Graphix Mode.
  194.  
  195.  
  196. This mode of operation is performed by the SetVideoMediaGraphicsMode() Movie Toolbox function. This functions by altering the operation of a track which contains video media.  The section of code below is called when this mode is selected and operates by stepping through all of the current movie's tracks and if it finds a video track alters the way that the media is displayed.
  197.  
  198.         theTrackCount = GetMovieTrackCount((**theDocHndl).theMovie);
  199.         for (count=1;count <=theTrackCount;count++)
  200.   {
  201.    movieTrack = GetMovieIndTrack((**theDocHndl).theMovie,count);
  202.             theMedia = GetTrackMedia(movieTrack);
  203.    GetMediaHandlerDescription(theMedia,&theMediaType,nil,nil);
  204.             if (theMediaType == VideoMediaType)
  205.                 if (SetVideoMediaGraphicsMode(GetMediaHandler(theMedia),
  206.         transparent,&gKeyColor))
  207.                     DisplayAlert (rGenAlert,rErrMessages,1);
  208.   }
  209.  
  210. Modifier Track mode.
  211.  
  212. This chromakey mode is only available of Systems running Quicktime 2.1 or later as it relys of the use of modifier tracks. Essentially, it is a different method of implementing the Graphix Mode as intead of directly calling   SetVideoMediaGraphicsMode(), this method modifies the movie video track media to operate in the same way.
  213.  
  214.  
  215. This sets the modifier data structure.
  216.  
  217.         theTrackCount = GetMovieTrackCount((**theDocHndl).theMovie);
  218.         theModifierStruct.graphicsMode = transparent;
  219.         theModifierStruct.opColor = gKeyColor;
  220.         theModifierTrack = GetMovieIndTrack((**theDocHndl).theMovie,theTrackCount+1);
  221.  
  222.  
  223. The tracks of the movie are then traversed and the first video track is altered to include a track reference to the modifier track.
  224.  
  225.         for (count=1;count <=theTrackCount;count++)
  226.         {
  227.             theTrack = GetMovieIndTrack((**theDocHndl).theMovie,count);
  228.             theMedia = GetTrackMedia(theTrack);
  229.             GetMediaHandlerDescription(theMedia,&theMediaType,nil,nil);
  230.             if (theMediaType == VideoMediaType)
  231.             {
  232.                 AddTrackReference(theTrack,theModifierTrack,
  233.                                   kTrackModifierTypeGraphicsMode,&theTrackIndex);
  234.                 count = theTrackCount;    // bump track count
  235.             }
  236.     
  237.  
  238. An input map for the video media is created and the modifier track information added.
  239.  
  240.         // create and add the input map
  241.             GetMediaInputMap(theMedia,&inputMap);
  242.             QTInsertChild( inputMap, kParentAtomIsContainer, kTrackModifierInput, theTrackIndex,
  243.                   0, 0, nil,&inputAtom);
  244.                   
  245.             QTInsertChild( inputMap, inputAtom, kTrackModifierType, 1, 0, 
  246.                    sizeof(kTrackModifierTypeGraphicsMode), &inputType, nil );
  247.                    
  248.             SetMediaInputMap(theMedia, inputMap );
  249.             QTDisposeAtomContainer(inputMap );
  250.     
  251.         }
  252.  
  253.  
  254. struct MovieDoc
  255. {
  256.    MovieController        theController;
  257.    Movie            theMovie;
  258.    long             theModifierIndex;
  259.    FSSpec           theFileSpec;
  260.    short            theMovieResID;
  261. };
  262. typedef struct MovieDoc MovieDoc, *MovieDocPtr, **MovieDocHndl;
  263.  
  264.  
  265. The MovieDoc structure listed above and defined in the ChromaKeyMovie.app.h file is implemented within the application to store all of the movie specific information of a given window and is created within the OpenCWindow() function in the windows.cp file using the following code:
  266. theDocHndl = (MovieDocHndl)NewHandle(sizeof(MovieDoc));
  267. and the following code will retrieve this information from a given WindowPtr:
  268. theDocHndl = (MovieDocHndl)GetWRefCon(theWindow);
  269.  
  270. The theMovie, theFileSpec and theMovieResID fields contain information specific to the movie displayed in the window and are filled within the LoadOneMovie() function defined in Test.cp, after the user selects a movie file.
  271.  
  272. The theController field is stores a reference to the movie controller attached to the movie.
  273. The theModifierIndex field stores a reference to the modifier track and is used by the Modifier Track Keying Mode menu option. This is also specific to versions of QuickTime from 2.1.
  274.  
  275.  
  276. Further enhancements and improvements.
  277.  
  278. Hopefully, this application will provide useful as a starting point for your experiments and I've listed below a few pointers where improvements and enhancements could be made.
  279.  
  280. •    The inclusion of a menu option to enable the user to select a point from the currently displayed movie frame as the color which is to be used for the Chromakey functions. 
  281.  
  282. •  An option to provide for the chromakeyed movie to be saved as a separate file thus enabling  processor intensive operations to be performed in non realtime on a wide range of Macintoshes with the final modified movie available for playback without the need to run the movie within the ChromaKeyMovie application.
  283.  
  284. •    Enhancement to the background image to allow for user selection of the background image. This can be further expanded to play multiple video tracks or movies layered over each other.
  285.  
  286. •   The use of a custom color search routine with the Transparent Color Transfer method to provide chroma keying on a color range centered around the selected color. As this would require additional processor cycles and will reduce the movies playback frame rate, this option would be of most use in conjunction with the second point. 
  287.  
  288. Jason Hodges-Harris, September 1995.
  289. © 1995 by Apple Computer, Inc. All rights reserved.
  290.  
  291.  
  292.